package com.dogkr.server.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dogkr.server.entity.Host;
import com.dogkr.server.entity.UserFavorites;
import com.dogkr.server.mapper.UserFavoritesMapper;
import com.sean.common.service.BaseService;

@Service
public class FavoritesService extends BaseService {
	@Autowired
	private UserFavoritesMapper favoritesMapper;
	
	public int insert(Long hostId, Long userId) {
		UserFavorites record = new UserFavorites();
		record.setHostId(hostId);
		record.setUserId(userId);
		record.setCreateDate(new Date());
		return favoritesMapper.insert(record);
	}
	
	public int deleteByPrimaryKey(Long id, Long userId) {
		UserFavorites record = favoritesMapper.selectByPrimaryKey(id);
		if(record != null && record.getUserId().longValue() == userId) {
			return favoritesMapper.deleteByPrimaryKey(id);
		}
		return 0;
	}

	public boolean checkHasHost(Long hostId, Long userId) {
		return favoritesMapper.selectCount(this.getParamMap("hostId", hostId, "userId", userId)) > 0;
	}

	public void deleteFavorites(Long id, Long userId) {
		if(favoritesMapper.selectCount(this.getParamMap("id", id, "userId", userId)) > 0) {
			favoritesMapper.deleteByPrimaryKey(id);
		}		
	}
}
